//
//  MyPickerViewController.m
//  Ghac 4S
//
//  Created by apple on 11-11-23.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//
#import "Global.h"
#import "MyPickerViewController.h"

@implementation MyPickerViewController
@synthesize pickerView;
@synthesize toolBar;
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
        m_array = nil;
    }
    return self;
}
-(void)dealloc
{
    KRELEASE(m_array);
    [self viewDidUnload];
    [super dealloc];
}
-(void)setDataWithArrays:(NSArray*)array, ...
{
    KRELEASE(m_array);
    m_array = [[NSMutableArray alloc] init];
    [m_array addObject:array];

    va_list argumentsList;
    va_start(argumentsList, array);
    NSArray* eachArray;
    while ((eachArray = va_arg(argumentsList, NSArray*)))
    {
        [m_array addObject:eachArray];
    }
    va_end(argumentsList);
    [self.pickerView reloadAllComponents];

//    [self.pickerView setShowsSelectionIndicator:YES];
}
-(void)show
{
    UIView* targetView = ((UIViewController*)[Global mainRootController]).view;
    [targetView addSubview:self.view];
    CGRect frame = self.view.frame;
    CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
    frame.origin.y = mainFrame.size.height;
    [self.view setFrame:frame];
    [UIView beginAnimations:@"pickerView" context:nil];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:targetView cache:YES];
    frame.origin.y = mainFrame.size.height - frame.size.height;
    [self.view setFrame:frame];
    [UIView commitAnimations];
}
-(void)hide
{
    UIView* targetView = ((UIViewController*)[Global mainRootController]).view;
    
    [targetView addSubview:self.view];
    CGRect frame = self.view.frame;
    CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
    [self.view setFrame:frame];
    [UIView beginAnimations:@"pickerView" context:nil];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(viewAnimationStop:)];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:targetView cache:YES];
    frame.origin.y = mainFrame.size.height;
    [self.view setFrame:frame];
    [UIView commitAnimations];
    
    
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.delegate != nil)
    {
        if ([self.delegate respondsToSelector:@selector(myPickerViewSelectedIndex:inComponment:)])
         {
             [self.delegate myPickerViewSelectedIndex:row inComponment:component];
         }
    }
}
-(void)doSelectedSector:(SEL)sector
{
    m_sector = sector;
}
-(void)viewAnimationStop:(id)sender
{
    [self.view removeFromSuperview];
}
-(void)doSelected:(id)sender
{
    [self hide];
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return [m_array count];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [[m_array objectAtIndex:component] count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%@",[[m_array objectAtIndex:component] objectAtIndex:row]];
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.pickerView = nil;
    self.toolBar = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
